using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Blazing.BusinessObjects.Reservations;
using Blazing.BusinessObjects.Cms;
using Blazing.BusinessObjects.Queries;
using Blazing.BusinessObjects.RESTAgent;
using Blazing.BusinessObjects.Locations;

namespace Blazing.UI.Cms.Admin.Usercontrols.Extension
{
    public partial class EditAddress : System.Web.UI.UserControl
    {
        public string Address1
        {
            get { return TextBoxAddress1.Text; }
            set { TextBoxAddress1.Text = value; }
        }
        public string Address2
        {
            get { return TextBoxAddress2.Text; }
            set { TextBoxAddress2.Text = value; }
        }
        public string Address3
        {
            get { return TextBoxAddress3.Text; }
            set { TextBoxAddress3.Text = value; }
        }
        public string Suburb
        {
            get { return TextBoxSuburb.Text; }
            set { TextBoxSuburb.Text = value; }
        }
        public string Postcode
        {
            get { return TextBoxPostcode.Text; }
            set { TextBoxPostcode.Text = value; }
        }
        public AddressType AddressType
        {
            get { return SelectAddressType1.AddressType; }
            set { SelectAddressType1.AddressType = value; }
        }
        public State State
        {
            get { return SelectCountryState1.State; }
            set { SelectCountryState1.State = value; }
        }
        public Country Country
        {
            get { return SelectCountryState1.State.Country; }
            set {  }
        }
        public Geocode Geocode
        {
            get {
                Address addr = new Address()
                {
                    Address1 = Address1,
                    Address2 = Address2,
                    Address3 = Address3,
                    State = State,
                    Country = Country,
                    Postcode = Postcode,
                    Suburb = Suburb
                };

                Blazing.BusinessObjects.Cms.Geocode geo = RESTAgent.GetGoogleGeocode(addr);

                //override with something the user typed in
                if (!string.IsNullOrEmpty(TextBoxLatitude.Text) && !string.IsNullOrEmpty(TextBoxLongatude.Text))
                {
                    try
                    {
                        geo.Latitude = double.Parse(TextBoxLatitude.Text);
                        geo.Longatude = double.Parse(TextBoxLongatude.Text);
                    }
                    catch { } //if this fails, then it will fall back to what come back from google
                }

                if (!string.IsNullOrEmpty(addr.Postcode))
                    TextBoxPostcode.Text = addr.Postcode;
                if (!string.IsNullOrEmpty(addr.Address1))
                    TextBoxAddress1.Text = addr.Address1;
                if (!string.IsNullOrEmpty(addr.Suburb))
                    TextBoxSuburb.Text = addr.Suburb;

                return geo;
            }
            set {
                TextBoxLatitude.Text = value.Latitude+"";
                TextBoxLongatude.Text = value.Longatude+"";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LinkButtonGetGeoInfo_Click(object sender, EventArgs e)
        {
            this.Geocode = this.Geocode;
        }
    }
}
